package edu.swust.graduation_project.eauction.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import edu.swust.graduation_project.eauction.R

class OrderActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order)

        val buyer = intent.getStringExtra("buyer")
        val itemCreateTime = intent.getSerializableExtra("item_create_time").toString()
        var buyerPhone = intent.getStringExtra("buyerPhone")
        val itemName = intent.getStringExtra("item_name")
        var buyerAddress = intent.getStringExtra("buyer_address")
        val seller = intent.getStringExtra("seller")
        var sellerPhone = intent.getStringExtra("sellerPhone")
        var sellerExpressNumber = intent.getStringExtra("seller_express_number")
        val isBuy = intent.getBooleanExtra("isBuy", true)

        val mIvOrder = findViewById<ImageView>(R.id.iv_order_icon)
        val mTvOrderName = findViewById<TextView>(R.id.tv_order_name)
        val mTvBuyer = findViewById<TextView>(R.id.tv_order_buyer)
        val mTvBuyerPhone = findViewById<TextView>(R.id.tv_order_buyer_phone)
        val mTVAddress = findViewById<TextView>(R.id.tv_order_address)
        val mTvSeller = findViewById<TextView>(R.id.tv_order_seller)
        val mTVSellerPhone = findViewById<TextView>(R.id.tv_order_seller_phone)
        val mTvExpress = findViewById<TextView>(R.id.tv_order_express)
        val mBtnOrder = findViewById<Button>(R.id.btn_order)

        Glide.with(this)
            .load(
                "*" +
                        itemCreateTime.trim().replace(" ", "%20")
                            .replace(":", "%3A").replace(".0", "") +
                        ".png"
            )
            .into(mIvOrder)
        mTvOrderName.text = itemName
        mTvBuyer.text = buyer
        mTvBuyerPhone.text = buyerPhone
        mTVAddress.text = "收货地址：\n" + buyerAddress
        mTvSeller.text = seller
        mTVSellerPhone.text = sellerPhone
        mTvExpress.text = "快递单号：\n" + sellerExpressNumber
        if (buyerPhone.isNullOrBlank() || buyerAddress.isNullOrBlank()
            || sellerPhone.isNullOrBlank() || sellerExpressNumber.isNullOrBlank()){
            mBtnOrder.setOnClickListener {
                val intent=Intent(this,OrderEditActivity::class.java)
                intent.putExtra("buyer",buyer)
                intent.putExtra("item_create_time",itemCreateTime)
                intent.putExtra("buyerPhone",buyerPhone)
                intent.putExtra("itemName",itemName)
                intent.putExtra("buyerAddress",buyerAddress)
                intent.putExtra("seller",seller)
                intent.putExtra("sellerPhone",sellerPhone)
                intent.putExtra("sellerExpressNumber",sellerExpressNumber)
                intent.putExtra("isBuy",isBuy)
                startActivity(intent)
            }
        } else{
            mBtnOrder.isEnabled=false
            mBtnOrder.text="订单已经完成"
        }
    }
}